package com.example.miaoshademo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Positive;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Shenluw
 * created：2019/7/17 22:45
 */
@RestController
public class DebugController {

	private Logger log = LoggerFactory.getLogger(DebugController.class);

	@Autowired
	private GoodsRepository        goodsRepository;
	@Autowired
	private MiaoshaCache           miaoshaCache;
	@Autowired
	private MiaoshaOrderRepository miaoshaOrderRepository;

	@RequestMapping("/dump")
	public Object dump(@Positive Integer miaoshaId) {
		// 打印当前的秒杀信息和对应的订单结果

		Map<String, Object> map = new HashMap<>();

		MiaoshaData data = miaoshaCache.get(miaoshaId);

		map.put("cacheData", data);

		List<Goods> goods = goodsRepository.findAll();

		map.put("goods", goods);

		List<MiaoshaOrder> orders = miaoshaOrderRepository.findAll();

		map.put("orders", orders);


		log.info("--------------------- dump ---------------------");

		log.info("cache: \n{}", data);
		log.info("goods: \n{}", goods);
		log.info("orders: \n{}", orders);

		log.info("------------------------------------------------");

		return map;
	}
}
